package com.example.smartcity.ui.selfCenter;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

import com.example.smartcity.R;
import com.example.smartcity.ui.Adapter.ChangePwsAdapter;
import com.example.smartcity.ui.Bean.CPwsFormBean;
import com.example.smartcity.ui.Bean.ChangeBean;
import com.example.smartcity.ui.Bean.stateBean;
import com.example.smartcity.ui.Tools.File;
import com.example.smartcity.ui.Tools.OkHttpUtils;
import com.example.smartcity.ui.guide.login;
import com.google.android.material.snackbar.Snackbar;
import com.google.gson.Gson;

import java.io.IOException;
import java.util.ArrayList;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class ChangePassword extends AppCompatActivity {

    private ListView listView;
    private Button btn_Change;
    String[] items = new String[]{"请输入原始密码","输入新密码","输入确认密码"};
    ArrayList<ChangeBean> arrayList = new ArrayList<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_change_password);
        listView = findViewById(R.id.listView);
        btn_Change = findViewById(R.id.btn_Change);
        initView();
    }
    public void setBack(View view){
        this.finish();
    }
    public void initView(){
        SharedPreferences sharedPreferences = getSharedPreferences("selfMessage",MODE_PRIVATE);
        for (int i = 0; i < items.length; i++) {
            ChangeBean bean = new ChangeBean(items[i]);
            arrayList.add(bean);
        }
        listView.setAdapter(new ChangePwsAdapter(getBaseContext(),R.layout.changepwsitem,arrayList));
        btn_Change.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Gson gson = new Gson();
                String newUrl = OkHttpUtils.getBaseUrl(getBaseContext()) + "/system/user/resetPwd";
                CPwsFormBean pwsFormBean = new CPwsFormBean();

                EditText oldPws = listView.findViewWithTag(0);
                EditText newPwsOne = listView.findViewWithTag(1);
                EditText newPwsTwo = listView.findViewWithTag(2);
                Log.d("TAG", "onClick: "+sharedPreferences.getString("userID",null));
                Boolean bol = oldPws!=null&&newPwsOne!=null&&newPwsTwo!=null;
                if (bol&&newPwsOne.getText().toString().equals(newPwsTwo.getText().toString())){
                    pwsFormBean.setUserId(sharedPreferences.getString("userID",null));
                    pwsFormBean.setOldPwd(oldPws.getText().toString());
                    pwsFormBean.setPassword(newPwsOne.getText().toString());
                    String json = gson.toJson(pwsFormBean);
                    OkHttpUtils.getCall(json,newUrl,new File(getBaseContext()).readFile("Token")).enqueue(new Callback() {
                        @Override
                        public void onFailure(Call call, IOException e) {
                            Log.d("TAG", "onFailure: "+e.getMessage());
                            if (e.getMessage().equals("timeout")){
                                Snackbar.make(v,"网络连接超时",Snackbar.LENGTH_SHORT).show();
                            }
                        }

                        @Override
                        public void onResponse(Call call, Response response) throws IOException {
                            stateBean stateBean = new Gson().fromJson(response.body().string(), com.example.smartcity.ui.Bean.stateBean.class);
                            if (stateBean.getCode()==200){
                                Toast.makeText(ChangePassword.this, "修改成功", Toast.LENGTH_SHORT).show();
                                new  AlertDialog.Builder(getBaseContext())
                                        .setTitle("提示")
                                        .setMessage("因您修改密码，故此登录账号失效，请重新登录")
                                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                                            @Override
                                            public void onClick(DialogInterface dialog, int which) {
                                                Intent intent = new Intent(ChangePassword.this, login.class);
                                                startActivity(intent);
                                            }
                                        }).setCancelable(false)
                                        .show();
                            }
                        }
                    });
                }else{
                    Snackbar.make(v,"两次密码输入不一致"+newPwsOne.getText().toString()+newPwsTwo.getText().toString(),Snackbar.LENGTH_SHORT).show();
                }
            }
        });
    }
}